<?php

	/**
	*	NormalizeIT
	*	@author Michael Giddens (SilverBiology)
	*/
error_reporting(E_ALL ^ E_NOTICE);
	set_time_limit(90);
	ini_set("memory_limit","256M");
	ini_set('post_max_size', '32M');
	ini_set('upload_max_filesize', '32M');
	
	/**
	* auto_detect_line_endings allows for the follow csv formats
	*	1. Comma delimited CSV (*.csv)
	*	2. CSV (MacIntosh) (*.csv)
	*	3. CSV (MS-DOS)
	*/
	ini_set("auto_detect_line_endings", 1);

	require_once('class.normalizeit.php');
	$inputFolder = "input/";
	$tmpFolder = "tmp/";

	$time_start = microtime_float();
	
	if (isset($_FILES['input_file']['tmp_name'])) {
		$input_file = $_FILES['input_file']['tmp_name'];
	} else {
		// Files are only allowed to reside in input folder to keep from hacking attemps
		$input_file = $inputFolder . $_REQUEST['input'];  
		$input_file = str_replace('../', '', $input_file); // More hack checks
	}
	
	$output_file = $_REQUEST['output'];

	switch ($_REQUEST['cmd']) {
		case 'url':
			// Could extend the code to handle a remote file too.
			break;

		case 'download':
			
			if ( trim( $output_file ) != '' && file_exists($input_file) ) {
			
				if ( trim( $output_file ) == '' ) {
					print "Error - Output file cannot be blank. Use argument 'output'. ";
				} else {
					$norm = new NormalizeIT();
					if (isset($_REQUEST['lowest_rank']) && $_REQUEST['lowest_rank'] != "" && $_REQUEST['lowest_rank'] != "Auto Detect") {
						$norm->lowest_rank = strtoupper($_REQUEST['lowest_rank']);
					}
					if (isset($_REQUEST["file_delimiter"]) && ($_REQUEST["file_delimiter"] != "")) {
						$norm->file_delimiter = stripslashes($_REQUEST["file_delimiter"]);
						if ($norm->file_delimiter == 'tab') $norm->file_delimiter = "\t";
					}
					$norm->process( $input_file );
					$tmp_file = $tmpFolder . md5( @mktime() );
					$norm->write_file( $tmp_file );
					$norm->download_file( $tmp_file, $output_file );
					unlink($tmp_file);
					exit();
				}
				
			} else {
				print "Error - Not a valid input file.";
			}
			break;
			
		case 'file':
		
			$output_file = $_REQUEST['output'];
			
			if ( trim( $output_file ) != '' && file_exists($input_file) ) {
			
				if ( trim( $output_file ) == '' ) {
					print "Error - Output file cannot be blank. Use argument 'output'. ";
				} else {
					$norm = new NormalizeIT();
					if (isset($_REQUEST["file_delimiter"]) && ($_REQUEST["file_delimiter"] != "")) {
						$norm->file_delimiter = stripslashes($_REQUEST["file_delimiter"]);
						if ($norm->file_delimiter == 'tab') $norm->file_delimiter = "\t";
					}
					$norm->process( $input_file );
					$norm->write_file( $tmpFolder . $output_file );
				}
			} else {
				print "Error - Not a valid input file.";
			}
			break;

		default:
			print "Error - Not a valid command.";
			break;
			
	}
	
	function microtime_float() {
			list($usec, $sec) = explode(" ", microtime());
			return ((float)$usec + (float)$sec);
	}

	$time_end = microtime_float();
	$time = $time_end - $time_start;

	print "Exec duration: $time<br>";
	print round( memory_get_usage() * 0.0009) . "KB<br>";
	
?>